-module(element_ceditor).
-compile(export_all).

-include_lib("nitrogen/include/wf.inc").
-include("elements.hrl").

render(ControlID, R) ->
    ButtonID = wf:temp_id(),
    TblRowID = wf:temp_id(),
    TextID = ControlID,

    EvR = R#ceditor.callback,
    Delegate = get_delegate( EvR ),
    Postback = get_postback( EvR ),

    OutputClass = case R#ceditor.output_class of
		      same ->
			  R#ceditor.editor_class;
		      A ->
			  A
		  end,
    
    CEditor = [
	       #p{},
	       #textarea{ id=TextID, class=R#ceditor.editor_class }, #br{},
	       #button{ id=ButtonID, text="Evaluer" },
	       
	       #p{},
	       #table {class=OutputClass, 
		       rows=[
			     #tablerow { cells=[
						#tablecell { id=TblRowID }
					  ]}
			    ]}
	      ],

    Container = #panel{ body = CEditor, class= R#ceditor.class },

    wf:wire(ButtonID, #event{delegate=?MODULE, type=click,  postback= {eval, TextID, TblRowID}}),
    wf:wire(ButtonID, #event{delegate=Delegate, type=click, postback= {Postback, TextID}} ),

    element_panel:render(wf:temp_id(), Container).

event( {eval, TextID, Update} ) ->
    wf:insert_top(Update, #panel{body = interpreter:runhertz( wf:q(TextID) ) }).
